package pure.engine.behave.observe 
{
	import flash.geom.Rectangle;
	import pure.utils.DesA;
	import pure.utils.DesN;
	import pure.engine.entity.Entity;
	import pure.engine.entity.EntityManager;
	import pure.engine.core.ProcessManager;
	import pure.engine.world.WorldManager;
	import pure.engine.behave.core.Behavior;
	
	/**
	 * 观察行为
	 */
public class ObserveBehavior extends Behavior 
{
	public function ObserveBehavior(radius:Number,targetNames:Array ) {
		this.radius = radius;
		_targetNames = targetNames;
	}
	
	
	// ----------------------------------［ Model ］----------------------------------
	
	protected var _target:Entity;
	protected var _radius:Number;    // 半径范围
	protected var _targetNames:Array;

	
	
	
	
	override public function destroy():void {
		super.destroy();
		_target = null;
		_targetNames = null;
	}
	
	
	override public function execute(elapseT:Number):void {
		// 目标存在，检查是否在范围内
		if (_target) {
			var dist:Number = DesN.getDistance(_owner.x, _owner.y, _target.x, _target.y);
			// 移除目标
			if (dist > _radius) {
				_target = null;
			}
		}
		
		// 不存在，寻找目标
		if (!_target) {

			var list:Array = EntityManager.instance.getEntitiesByNames(_targetNames);
			if (list == null) return;
			var entity:Entity = null;
			var len:int = list.length;
			for (var i:int = 0; i < len; i++) {
				entity = list[i];

				dist = DesN.getDistance(_owner.x, _owner.y, entity.x, entity.y);
				if (dist <= _radius) {
					_target = entity;
					break;
				}
			}
		}
		
		// 最终确认目标，(自定义)处理
		if (_target) {
			_owner.angle = DesN.getDegreesByPoint(_owner.position, _target.position);
			var frame:int = DesN.parseFrameByDegrees(_owner.angle);
			_owner.body.pointer = frame;
			//_doActionToTarget();
		}
		
	}
	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  get × set  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	 
	 
	 
	public function get radius():Number { return _radius; }
	public function set radius(value:Number):void {
		_radius = value;
	}
	 
	 
	 
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	 
	
	protected function _doActionToTarget():void {
		trace('attack!!');
	}
	 
	 

}

}